package org.mapdroid.data;

import java.util.List;

import org.mapdroid.data.adt.*;

import android.content.Context;
import android.content.res.Resources.NotFoundException;

import com.google.android.maps.GeoPoint;

import java.io.*;
import java.util.*;

/*
 * SERVICE FORMAT
 * <INT:CATEGORY>,<STRING:NAME>,<STRING:ADDRESS>,<STRING:PHONE>,<STRING:DESCRIPTION>,<STRING:PHOTO>,[<STRING:GALLERY>-...],<STRING:LINK>,<INT:LAT>,<INT:LON>
 * ...
 * PARKING FORMAT
 *
 */

public class DataLoader {
	private List<MdService> DataLoad;
	private List<Client> users;
	private Context context;
	
	public DataLoader(Context context){
		this.context = context;
		DataLoad = new ArrayList<MdService>();
		users = new LinkedList<Client>();
	}
	
	//load txt file & put read information in LinkedList
	public synchronized void loadData(int fileResource) throws NotFoundException, IOException{
			InputStreamReader isr = new InputStreamReader(context.getResources().openRawResource(fileResource));
			BufferedReader file=new BufferedReader(isr);
			StringTokenizer ch=new StringTokenizer(file.readLine(),";");
			
			while(ch.hasMoreTokens()){
				int category = Integer.parseInt(ch.nextToken().trim());
				String name = ch.nextToken().trim();
				String address = ch.nextToken().trim(); 
				String phoneNumber = ch.nextToken().trim();
				String description = ch.nextToken().trim(); 
				int photo = -1;
				try{
					photo = Integer.parseInt(ch.nextToken().trim()); 
				}catch(NumberFormatException e){}
				
				String link = ch.nextToken().trim();
				int lat = -1;
				int lon = -1;
				try{
					lat = (int) (Double.parseDouble(ch.nextToken().trim()) * 1E6);
				} catch (NumberFormatException e){}
				try{
					lon = (int) (Double.parseDouble(ch.nextToken().trim()) * 1E6);
				} catch (NumberFormatException e){}
				
				MdService tmp = null;
				
				switch(category){
					case MdService.PARKING:
						int parkingNumber = -1;
						float pricePerHour = -1;
						try{
							parkingNumber = Integer.parseInt(ch.nextToken().trim());
						} catch(NumberFormatException e){}
						try{
							pricePerHour = Float.parseFloat(ch.nextToken().trim());
						} catch(NumberFormatException e){}
						
						tmp = new Parking(	name, 
											address, 
											phoneNumber, 
											description, 
											photo, 
											link, 
											new GeoPoint(lat, lon), 
											parkingNumber,
											pricePerHour);
						
						break;
						
					case MdService.RESTAURANT:
						tmp = new Restaurant(	name, 
												address, 
												phoneNumber, 
												description, 
												photo, 
												link, 
												new GeoPoint(lat, lon)	);
						break;
					case MdService.BEAUTY_CENTER:
						tmp = new BeautyCenter(	name, 
												address,
												phoneNumber,
												description,
												photo,
												link,
												new GeoPoint(lat, lon));
						break;
					case MdService.CLOTHING:
						tmp = new Clothing(	name,
											address,
											phoneNumber,
											description,
											photo,
											link,
											new GeoPoint(lat, lon));
						break;
					case MdService.GYM:
						tmp = new Gym(	name,
										address,
										phoneNumber,
										description,
										photo,
										link,
										new GeoPoint(lat, lon));
						break;
				}
				if(tmp!=null)
					DataLoad.add(tmp);
				String newLine = file.readLine();
				if(newLine!=null)
					ch=new StringTokenizer(newLine, ";");
			}
		
	}
	
	/*//load txt file & put read information in LinkedList
	public synchronized void writeData(int fileResource) throws NotFoundException, IOException{
			OutputStreamWriter isr = new OutputStreamWriter(context.);
			BufferedReader file=new BufferedReader(isr);
			StringTokenizer ch=new StringTokenizer(file.readLine(),";");
			
			while(ch.hasMoreTokens()){
				int category = Integer.parseInt(ch.nextToken().trim());
				String name = ch.nextToken().trim();
				String address = ch.nextToken().trim(); 
				String phoneNumber = ch.nextToken().trim();
				String description = ch.nextToken().trim(); 
				int photo = -1;
				try{
					photo = Integer.parseInt(ch.nextToken().trim()); 
				}catch(NumberFormatException e){}
				
				String link = ch.nextToken().trim();
				int lat = -1;
				int lon = -1;
				try{
					lat = (int) (Double.parseDouble(ch.nextToken().trim()) * 1E6);
				} catch (NumberFormatException e){}
				try{
					lon = (int) (Double.parseDouble(ch.nextToken().trim()) * 1E6);
				} catch (NumberFormatException e){}
				
				MdService tmp;
				
				switch(category){
					case MdService.BEAUTY_CENTER:
						break;
					case MdService.CLOTHING:
						break;
					case MdService.GYM:
						break;
					case MdService.PARKING:
						int parkingNumber = -1;
						float pricePerHour = -1;
						try{
							parkingNumber = Integer.parseInt(ch.nextToken().trim());
						} catch(NumberFormatException e){}
						try{
							pricePerHour = Float.parseFloat(ch.nextToken().trim());
						} catch(NumberFormatException e){}
						
						tmp = new Parking(	name, 
											address, 
											phoneNumber, 
											description, 
											photo, 
											link, 
											new GeoPoint(lat, lon), 
											parkingNumber,
											pricePerHour);
						
						DataLoad.add(tmp);
						break;
					case MdService.RESTAURANT:
						break;
				
				}
				String newLine = file.readLine();
				if(newLine!=null)
					ch=new StringTokenizer(newLine, ";");
			}
		
	}*/
	
	//load txt file & put read information in LinkedList
	public synchronized void loadUsers(int fileResource) throws NotFoundException, IOException{
			InputStreamReader isr = new InputStreamReader(context.getResources().openRawResource(fileResource));
			BufferedReader file=new BufferedReader(isr);
			StringTokenizer ch=new StringTokenizer(file.readLine(),";");
			
			while(ch.hasMoreTokens()){
				String username = ch.nextToken().trim();
				String password = ch.nextToken().trim();
				
				String tmp = ch.nextToken().trim();
				String name = tmp.substring(0, 1).toUpperCase() + tmp.substring(1, tmp.length());
				
				tmp = ch.nextToken().trim();
				String surname = tmp.substring(0, 1).toUpperCase() + tmp.substring(1, tmp.length());
				
				tmp = ch.nextToken().trim();
				String address = tmp.substring(0, 1).toUpperCase() + tmp.substring(1, tmp.length()); 
				
				String phone = ch.nextToken().trim();
				String mobilePhone = ch.nextToken().trim();
				String email = ch.nextToken().trim();
				
				Client client = new Client(	username,
											password,
											name,
											surname,
											address,
											phone,
											mobilePhone,
											email );						
				users.add(client);

				String newLine = file.readLine();
				if(newLine!=null)
					ch=new StringTokenizer(newLine, ";");
			}
		
	}
	
	
	//List return
	public List<MdService> getDataList(){
		return this.DataLoad;
	} 
	
	public List<Client> getUsersList(){
		return this.users;
	}
	
}
